package cac.fenjian.resource.msworkbinbasic.service;

import cac.common.DateUtil;
import cac.fenjian.resource.MsWorkbinBasic.dto.MsWorkbinBasicDTO;
import cac.fenjian.resource.msworkbinbasic.dao.MsWorkbinBasicDao;
import com.cac.dme.adapter.rest.msg.QueryReqBean;
import com.cac.dme.adapter.rest.msg.QueryRespBean;
import com.cac.dme.adapter.rest.msg.ResponseMsg;
import com.cac.dme.adapter.utils.ComUtil;
import com.cac.dme.adapter.utils.PageHelper;
import com.cac.dme.common.exception.service.DaoException;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.util.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class MsWorkbinBasicService {
    @Autowired
    private MsWorkbinBasicDao msWorkbinBasicDao;

    Date date = new Date();
    SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    String nowTime = sf.format(date);
    Date time = sf.parse(nowTime);

    public MsWorkbinBasicService() throws ParseException {
    }

    /**
     * 按照条件分页查询
     * @param queryReqBean
     * @param orderBy
     * @return
     */
    @Transactional
    public QueryRespBean<MsWorkbinBasicDTO> searchMsWorkbinBasicByPage(QueryReqBean<MsWorkbinBasicDTO> queryReqBean, String orderBy, String Keywords){
        QueryRespBean<MsWorkbinBasicDTO> queryRespBean = new QueryRespBean<>();
        try{
            PageHelper.startPage(queryReqBean.getPageParameter());
            MsWorkbinBasicDTO searchParams = queryReqBean.getSearchParams();
            Page<MsWorkbinBasicDTO> dataList = msWorkbinBasicDao.searchMsWorkbinBasicByPage(searchParams,orderBy,Keywords);
            queryRespBean.setResult(dataList);
            return queryRespBean;
        }catch (Exception e){
            throw  new DaoException(e.getMessage(),e);
        }
    }
    //判断查询是否为空
    @Transactional
    public List<MsWorkbinBasicDTO> searchMsWorkbinBasicByPage(MsWorkbinBasicDTO searchParams, String orderBy, String Keywords){
        try{
            List<MsWorkbinBasicDTO> dataList = msWorkbinBasicDao.searchMsWorkbinBasicByPage(searchParams,orderBy,Keywords);
            return dataList;
        }catch (Exception e){
            throw  new DaoException(e.getMessage(),e);
        }
    }
    @Transactional
    public QueryRespBean<Map<String,String>> searchMsWorkbin(QueryReqBean<MsWorkbinBasicDTO> queryReqBean, String orderBy, String Keywords){
        QueryRespBean<Map<String,String>> queryRespBean = new QueryRespBean<>();
        try{
            PageHelper.startPage(queryReqBean.getPageParameter());
            MsWorkbinBasicDTO searchParams = queryReqBean.getSearchParams();
            Page<Map<String,String>> dataList = msWorkbinBasicDao.searchMsWorkbin(searchParams,orderBy,Keywords);
            queryRespBean.setResult(dataList);
            return queryRespBean;
        }catch (Exception e){
            throw  new DaoException(e.getMessage(),e);
        }
    }
    /**
     * 通过主键查询单条记录
     *
     * @param id 主键id
     * @return MsWorkbinBasicDTO
     */
    @Transactional
    public MsWorkbinBasicDTO findMsWorkbinBasicById(String id) {
        try {
            MsWorkbinBasicDTO msWorkbinBasicDTO = msWorkbinBasicDao.findMsWorkbinBasicById(id);
            //记录日志
            if (msWorkbinBasicDTO != null) {
            }
            return msWorkbinBasicDTO;
        } catch (Exception e) {
            throw new DaoException(e.getMessage(), e);
        }
    }
    /**
     * 新增单个数据
     * @param
     * @return
     */
    @Transactional
    public int insertMsWorkbinBasic(Map map) {
        try{
            int count = msWorkbinBasicDao.insertMsWorkbinBasicDTO(map,ComUtil.getId(),DateUtil.nowTime());
            return count;
        }catch (Exception e){
            throw new DaoException(e.getMessage(),e);
        }
    }
    /**
     *修改数据
     * @param map
     * @return
     */
    @Transactional
    public int updateMsWorkbinBasic(Map map){
        try{
            int count = msWorkbinBasicDao.updateMsWorkbinDTO(map,DateUtil.nowTime());
            if(count==0){
                throw new DaoException("修改数据失败");
            }
            return count;
        }catch (Exception e){
            throw new DaoException(e.getMessage(),e);
        }
    }
    /**
     * 按主键单条删除
     *
     * @param id
     * @return
     */
    @Transactional
    public int deleteMsWorkbinBasicById(String id) {
        if (StringUtils.isEmpty(id)) {
            throw new DaoException("删除失败！传入的参数主键为null");
        }
        try {
            //记录日志
            MsWorkbinBasicDTO msWorkbinBasicDTO = msWorkbinBasicDao.findMsWorkbinBasicById(id);
            if (msWorkbinBasicDTO == null) {
                throw new DaoException("删除失败！对象不存在");
            }
//删除业务数据
            int count = msWorkbinBasicDao.deleteMsWorkbinBasicById(id);
            return count;
        } catch (Exception e) {
            throw new DaoException(e.getMessage(), e);
        }
    }

    public PageInfo queryWorkbinMaterial(HashMap queryMap, int currentPage, int pageSize){
        com.github.pagehelper.PageHelper.startPage(currentPage,pageSize);
        PageInfo pageinfo = new PageInfo(msWorkbinBasicDao.queryWorkbinMaterial(queryMap));
        return pageinfo;
    }

    @Transactional
    public ResponseMsg updateWorkbinMaterial(HashMap map){
        ResponseMsg responseMsg = new ResponseMsg();
        try {
            msWorkbinBasicDao.updateWorkbinMaterial(map);
            responseMsg.setRetCode("200").setMessage("操作成功");
        }catch (Exception e){
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            responseMsg.setRetCode("201").setMessage("操作失败");
        }
        return  responseMsg;
    }
    @Transactional
    public ResponseMsg updateSetEmptyBoxes(){
        ResponseMsg responseMsg = new ResponseMsg();
        try {
            msWorkbinBasicDao.updateSetEmptyBoxes();
            responseMsg.setRetCode("200").setMessage("操作成功");
        }catch (Exception e){
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            responseMsg.setRetCode("201").setMessage("操作失败");
        }
        return  responseMsg;
    }
}
